package com.example.spring.dao;

import com.example.spring.util.JDBCUtil;
import com.example.spring.task.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDaoImpl implements UserDao{

    User user=null;

    @Override
    public User login(String username) {



        try{
            Connection conn = JDBCUtil.getConn();

            String sql="select * from login where username=?";
            PreparedStatement statement = conn.prepareStatement(sql);

            statement.setString(1,username);

            ResultSet rs = statement.executeQuery();

            if (!rs.next()){
                return user;
            }
            user=new User(rs.getString("id"),rs.getString("username"), rs.getString("password"));

            return user;
        }catch (Exception e){
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public int register(String username, String password) {
        int i=0;
        try{
            Connection conn = JDBCUtil.getConn();
            String sql="insert into login(username,password) values(?,?)";
            PreparedStatement statement = conn.prepareStatement(sql);
            statement.setString(1,username);
            statement.setString(2,password);
            i = statement.executeUpdate();
            return i;

        }catch (Exception e){
            e.printStackTrace();
        }
        return i;
    }
}
